﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Patchworker
{
	public enum ERepeatType{ Normal, Reverse, PingPong, Once} 

	public partial class AnimationPreviewWindow : Form
	{

		bool userTrackBarChanged = false;

		SpritePanel sprite = new SpritePanel();

		public AnimationPreviewWindow()
		{
			ConfigContainer.Instance.refreshHandler += refresh;
			InitializeComponent();
			GDIExtension.UsefulAPI.putEnumsInCombobox<ERepeatType>(this.RepeatTypeComboBox);
			this.RepeatTypeComboBox.SelectedItem = ERepeatType.Normal;

			this.sprite.Redraw += new EventHandler(sprite_Redraw);

			this.sprite.OnMouseDown += new EventHandler<MouseEventArgs>(sprite_MouseDown);
			this.sprite.OnMouseEnter += new EventHandler(sprite_OnMouseEnter);
			this.sprite.OnMouseLeave += new EventHandler(sprite_OnMouseLeave);

			this.AnimationPanel.MouseMove += new MouseEventHandler(AnimationPanel_MouseMove);
			this.AnimationPanel.MouseDown += new MouseEventHandler(AnimationPanel_MouseDown);
			this.AnimationPanel.MouseUp += new MouseEventHandler(AnimationPanel_MouseUp);
			this.AnimationPanel.MouseLeave += new EventHandler(AnimationPanel_MouseLeave);

			this.AnimationPanel.Paint += new PaintEventHandler(AnimationPanel_Paint);
		}
		void AnimationPanel_MouseLeave(object sender, EventArgs e)
		{
			sprite_OnMouseLeave(sender, e);
			sprite.Ungrab();
		}

		void sprite_OnMouseLeave(object sender, EventArgs e)
		{
			this.Cursor = Cursors.Default;
		}

		void sprite_OnMouseEnter(object sender, EventArgs e)
		{

			this.Cursor = Cursors.SizeAll;
		}

		public bool IsPlaying
		{
			get
			{
				return this.sprite.IsPlaying;
			}
			set
			{
				this.setPlaying(false);
			}
		}


		void AnimationPanel_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;

			if (ConfigContainer.Instance.RenderBackgroundImage)
			{
				if (ConfigContainer.Instance.BackgroundImage != null)
					g.DrawImage(ConfigContainer.Instance.BackgroundImage, 0,0, ConfigContainer.Instance.BackgroundImage.Width, ConfigContainer.Instance.BackgroundImage.Height);
			}
			sprite.render(g);
			//System.Diagnostics.Debug.WriteLine(DateTime.Now);
		}


		
		public void refresh()
		{
			this.AnimationPanel.BackColor = ConfigContainer.Instance.BackgroundColor;

			/*
			if (ConfigContainer.Instance.RenderBackgroundImage)
			{
				if(ConfigContainer.Instance.BackgroundImage!=null)
				this.AnimationPanel.BackgroundImage = ConfigContainer.Instance.BackgroundImage;
			}
			else this.AnimationPanel.BackgroundImage = null;
			
			 */

			FrameRateNumberBox_ValueChanged(null, null);

			AnimationTrackBar.Maximum = ConfigContainer.Instance.NumFullFrames - 1;

			sprite.CurrentFrame = (int)AnimationTrackBar.Value;

			if (ConfigContainer.Instance.ImageLoaded)
			{
				sprite.refresh();
			}


			AnimationPanel.Invalidate();

		}
		

		private void RepeatTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			sprite.RepeatType = (ERepeatType)this.RepeatTypeComboBox.SelectedItem;
		}

		private void FrameRateNumberBox_ValueChanged(object sender, EventArgs e)
		{
			sprite.Interval = (int)Math.Round(1000 / FrameRateNumberBox.Value);
		}

		private void PlayButton_Click(object sender, EventArgs e)
		{
			setPlaying(!sprite.IsPlaying);
		}

		private void setPlaying(bool isPlaying)
		{
			if (isPlaying == true)
			{
				PlayButton.Text = "PAUSE";
				sprite.IsPlaying = true;
			}
			else
			{
				PlayButton.Text = "PLAY";
				sprite.IsPlaying = false;
			}
		}

		private void AnimationTrackBar_MouseDown(object sender, MouseEventArgs e)
		{
			setPlaying(false);
		}

		private void AnimationTrackBar_ValueChanged(object sender, EventArgs e)
		{
			if (userTrackBarChanged)
			{
				sprite.CurrentFrame = AnimationTrackBar.Value;

				this.AnimationPanel.Invalidate(sprite.DisplayRectangle(3));
			}
		}



		void sprite_Redraw(object sender, EventArgs e)
		{
			userTrackBarChanged = false;
			AnimationTrackBar.Value = sprite.CurrentFrame;
			userTrackBarChanged = true;

			this.AnimationPanel.Invalidate(sprite.DisplayRectangle(3));
		}

		#region drag

	//	Rectangle traceRect = new Rectangle();

		protected bool isDragging = false;

		protected Point clickPosition;
		
		void sprite_MouseDown(object sender, MouseEventArgs e)
		{
			this.clickPosition.X = e.X;
			this.clickPosition.Y = e.Y;

			isDragging = true;
			//System.Diagnostics.Debug.WriteLine("Clicked Bias : " + this.clickPosition);
		}
			void AnimationPanel_MouseUp(object sender, MouseEventArgs e)
		{
			sprite.onMouseUp(e.Location, e.Button);

			isDragging = false;
		}

		void AnimationPanel_MouseDown(object sender, MouseEventArgs e)
		{
			sprite.onMouseDown(e.Location, e.Button);
		}

		void AnimationPanel_MouseMove(object sender, MouseEventArgs e)
		{
			sprite.onMouseMove(e.Location, e.Button);

			if (e.Button == MouseButtons.Left && isDragging)
			{

				Point screenMousePoint = e.Location;
				this.sprite.setPosition(screenMousePoint.X - clickPosition.X, screenMousePoint.Y - clickPosition.Y);
			}
		}
		#endregion

		private void CenterSpriteButton_Click(object sender, EventArgs e)
		{
			sprite.setPosition((this.AnimationPanel.Width - this.sprite.Width) >> 1, (this.AnimationPanel.Height - this.sprite.Height) >> 1);
		}
	}
}
